package com.mall.goods.service.impl;

import com.mall.goods.model.Brand;
import com.mall.goods.mapper.BrandMapper;
import com.mall.goods.service.IBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author Darren
 * @since 2022-08-18
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addBrand(Brand brand){
        return super.save(brand);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteById(Integer id){
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBrand(Brand brand){
        return super.updateById(brand);
    }

    @Override
    public Brand getById(Integer id){
        return super.getById(id);
    }

    @Override
    public List<Brand> selectList(Brand brand){
        return super.list(getQueryWrapper(brand));
    }

    @Override
    public Page<Brand> selectPage(Page<Brand> page, Brand brand){
        return super.page(page, getQueryWrapper(brand));
    }

    public LambdaQueryWrapper<Brand> getQueryWrapper(Brand brand){
        return Wrappers.<Brand>lambdaQuery()
            .eq(ObjectUtils.isNotEmpty(brand.getId()), Brand::getId, brand.getId())
            .eq(ObjectUtils.isNotEmpty(brand.getName()), Brand::getName, brand.getName())
            .eq(ObjectUtils.isNotEmpty(brand.getImage()), Brand::getImage, brand.getImage())
            .eq(ObjectUtils.isNotEmpty(brand.getInitial()), Brand::getInitial, brand.getInitial())
            .eq(ObjectUtils.isNotEmpty(brand.getSort()), Brand::getSort, brand.getSort())
        ;
    }
}
